package com.unionftech.crm_android.ui

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.unionftech.common.R.string
import com.unionftech.common.base.IUpdate
import com.unionftech.common.base.mvp.annitation.InjectPresenter
import com.unionftech.common.base.mvp.impl.BaseMvpActivity
import com.unionftech.common.bean.ResponseResult
import com.unionftech.common.common.GlobalInfoManager
import com.unionftech.common.common.bean.AppVersionInfo
import com.unionftech.common.common.contract.CommonContract
import com.unionftech.common.common.language.DynamicResourceManager
import com.unionftech.common.common.presenter.CommonPresenter
import com.unionftech.common.router.RouterPath
import com.unionftech.common.utils.LogUtils
import com.unionftech.common.utils.Utils
import com.unionftech.crm_android.R

@Route(
    path = RouterPath.UFUFMainStandardActivityInfo.PATH,
    name = RouterPath.UFUFMainStandardActivityInfo.NAME
)
class UFMainStandardActivity : BaseMvpActivity(), CommonContract.IUpdateView, IUpdate {
    private val mMainBottomNavigation by lazy { findViewById<BottomNavigationView>(R.id.main_bottom_navigation) }

    @InjectPresenter
    private lateinit var mCommonPresenter: CommonPresenter
    private val mUpdateVersion by lazy {
        mCommonPresenter.getUpdateInfo()
        true
    }

    companion object {
        private const val TAG = "UFMainStandardActivity"

        @JvmStatic
        fun startThis(context: Context) {
            Intent(context, UFMainStandardActivity::class.java).apply {
                if (context !is Activity) {
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }
                context.startActivity(this)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun initLayout() = R.layout.activity_ufmain_standard

    override fun setListener() {
        super.setListener()
        GlobalInfoManager.getInstance().addListener(this)
    }

    override fun initLanguageText() {
        super.initLanguageText()
        mMainBottomNavigation.menu.findItem(R.id.homeFragment).title =
            DynamicResourceManager.getString(mContext, string.home)
        mMainBottomNavigation.menu.findItem(R.id.depositFragment).title =
            DynamicResourceManager.getString(mContext, string.deposit)
        mMainBottomNavigation.menu.findItem(R.id.mineFragment).title =
            DynamicResourceManager.getString(mContext, string.personal)
    }

    override fun setView() {
        super.setView()
        hideTitleBar()
        setupBottomNavigation()
    }


    private fun setupBottomNavigation() {
        // 确保 NavHostFragment 已初始化
        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.main_fragment_container_view) as NavHostFragment
        val navController = navHostFragment.navController
        // 设置底部导航
        mMainBottomNavigation.setupWithNavController(navController)
        mMainBottomNavigation.itemIconTintList = null
        mMainBottomNavigation.itemTextColor = mContext.getColorStateList(R.color.nav_color_selector)
    }

    override fun loadData() {
        super.loadData()
        mCommonPresenter.getUpdateInfo()
    }

    override fun handleUpdateView(response: ResponseResult<AppVersionInfo>) {
        if (response.code == ResponseResult.SUCCESS_CODE) {
            response.data?.also {
                updateView(it)
            }
        } else {
            LogUtils.d(TAG, "handleUpdateView error->$response")
        }
    }

    private fun updateView(appVersionInfo: AppVersionInfo) {
        val versionName = "V${Utils.getVersionName(mContext)}"
        LogUtils.d(TAG, "app current version-->$versionName")
        LogUtils.d(TAG, "service current version-->${appVersionInfo.versionCode}")
        if (Utils.compareVersions(appVersionInfo.versionCode, versionName) > 0) {
            mRootView.postDelayed({
                //跳转更新
                UFUpdateActivity.startThis(mContext, appVersionInfo)
            }, 1000)
        }
    }

    override fun onUpdate(obj: Any?) {
        if (obj is Int) {
            if (obj == GlobalInfoManager.UPDATE_VERSION_INFO) {
                mUpdateVersion
            }
        }
    }

    override fun onDestroy() {
        GlobalInfoManager.getInstance().remove(this)
        super.onDestroy()
    }
}